package com.enjoy.utils;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

public class GPS {

	public static Location getLocation(Activity activity){
		 final LocationManager locationManager;
	        String context = Context.LOCATION_SERVICE;
	        locationManager = (LocationManager)activity.getSystemService(context);
	        //String provider = LocationManager.GPS_PROVIDER;
	        
	        Criteria criteria = new Criteria();
	        
	        criteria.setAccuracy(Criteria.ACCURACY_FINE);
	        criteria.setAltitudeRequired(true);
	        criteria.setBearingRequired(true);
	        criteria.setCostAllowed(true);
	        criteria.setPowerRequirement(Criteria.POWER_HIGH);
	      
	        
	        LocationListener locationListener = new LocationListener() {
		    	public void onLocationChanged(Location location) {
		    		if(location==null)
		    		{Log.i("location is null","fuck");}
		    		
		    		Log.i("location", "has changed!!!!");
					updateWithNewLocation(location);
				
		    	}
		    	public void onProviderDisabled(String provider){
		    	
					updateWithNewLocation(null);
					Log.e("provider", "Disabled");
				
		    	}
		    	public void onProviderEnabled(String provider){ 
		    		updateWithNewLocation(null);
					Log.e("provider", "Enabled");
		    	}
		    	public void onStatusChanged(String provider, int status,
		    	Bundle extras){ }
		    };
	        
		    
	        final String provider = locationManager.getBestProvider(criteria, true);	
		
		
	        
	        if(provider!=null){
	        	Log.i("provider is:",provider);
	        }else{
	        	Log.e("provider", "null");
	        }
	             locationManager.requestLocationUpdates(provider, 1000, 1,
	        		locationListener);
	             
	        Location location = locationManager.getLastKnownLocation(provider);
	        location = locationManager.getLastKnownLocation(provider);
	        location = locationManager.getLastKnownLocation(provider);
	        location = locationManager.getLastKnownLocation(provider);
	        location = locationManager.getLastKnownLocation(provider);
	        if(location==null){
	        	if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
	        	Log.i("GPS location is null"," ");
	        	location=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);}
	        	else {
					Log.i("network provider","is not enabled");
				}
	        }
	        
	        updateWithNewLocation(location);
	        
	        return location;
		       
   }
	   	
	        
	        
		       
	    
	       
	        
	   	
	

	private static void updateWithNewLocation(Location location){

    	String latLongString;
    	
    	
    	if (location != null) {
    	    double lat = location.getLatitude();
    	    double lng = location.getLongitude();
    	    latLongString = "lat:" + lat + " lng:" + lng;
    	    Log.i("location is changed",latLongString);
    	    
    	} else {
    	    latLongString = "location is null";
    	    Log.i("location","null");

    	}	
    }

}
